using System;
using System.Configuration;


namespace BIUtilities.Publish.CustomConfiguration
{
    /// <summary>
    /// Parent configuration element for changes made to a PAS report or reports
    /// </summary>
    public class PasElement :
        ConfigurationElement, IUpdatableConfig
    {
        /// <summary>
        /// Target PAS webservice
        /// </summary>
        [ConfigurationProperty("targetWebservice", IsKey = false, IsRequired = true)]
        public string TargetWebservice
        {
            get { return this["targetWebservice"].ToString(); }
            set { this["targetWebservice"] = value; }
        }

        /// <summary>
        /// Library to load reports from
        /// </summary>
        [ConfigurationProperty("matchOnLibrary", IsKey = false, IsRequired = true)]
        public string MatchOnLibrary
        {
            get { return this["matchOnLibrary"].ToString(); }
            set { this["matchOnLibrary"] = value; }
        }

        /// <summary>
        /// Book to load reports from (optional)
        /// </summary>
        [ConfigurationProperty("matchOnBook", IsKey = false, IsRequired = false)]
        public string MatchOnBook
        {
            get { return this["matchOnBook"].ToString(); }
            set { this["matchOnBook"] = value; }
        }

        /// <summary>
        /// Page to load (optional)
        /// </summary>
        [ConfigurationProperty("matchOnPage", IsKey = false, IsRequired = false)]
        public string MatchOnPage
        {
            get { return this["matchOnPage"].ToString(); }
            set { this["matchOnPage"] = value; }
        }

        /// <summary>
        /// List of datasource changes
        /// </summary>
        [ConfigurationProperty("dataSources", IsKey = true, IsRequired = false)]
        public PasDataSourceElementCollection PasSources
        {
            get { return (PasDataSourceElementCollection)this["dataSources"]; }
        }

        /// <summary>
        /// Whether this element is active and will be processed.
        /// </summary>
        [ConfigurationProperty("enabled", IsKey = false, IsRequired = false, DefaultValue = "true")]
        [RegexStringValidator(@"(^$)|([Tt][Rr][Uu][Ee])|([Ff][Aa][Ll][Ss][Ee])|(\[.*?\])")]
        private string Enabled
        {
            get { return (string)this["enabled"]; }
        }

        /// <summary>
        /// Public parameter exposed for boolean
        /// </summary>
        public bool IsEnabled
        {
            get
            {
                // This might be an injected value, so we try parsing it (and hope it's been replaced)
                // and if not, then we just disable it due to the error.
                bool retVal = false;
                if (bool.TryParse((string)this["enabled"], out retVal))
                {
                    return retVal;
                }
                else
                {
                    throw new ArgumentException(string.Format(
                        "Failed to parse property 'enabled' for {0}",
                        this.ElementInformation.Type.ToString()));
                }
            }
        }
        /// <summary>
        /// Makes this element editable
        /// </summary>
        /// <returns></returns>
        public override bool IsReadOnly()
        {
            return false;
        }


        #region IUpdatableConfig Members
        /// <summary>
        /// Creating a public accessor to get at the property values
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        object IUpdatableConfig.this[string propertyName]
        {
            get { return base[propertyName]; }
            set { base[propertyName] = value; }
        }
        #endregion

    }
}
